﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SYH.Config;

namespace SYH.User.DAL
{
   public  class UserMap
    {

        #region 用户地图Model
        /// <summary>
        /// 用户地图Model
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <returns></returns>
       public SYH.User.Model.UserMap GetModel(int UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserId,CityName,Lon,Lat,Clon,Clat,Remark,StaticMap,Zoom from User_Map ");
            strSql.Append(" where UserId=@UserId ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4)	
                                        };
            parameters[0].Value = UserId;

            SYH.User.Model.UserMap model = new Model.UserMap();
            DataSet ds = Common.SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["UserId"] != null && ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CityName"] != null && ds.Tables[0].Rows[0]["CityName"].ToString() != "")
                {
                    model.CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Lon"] != null && ds.Tables[0].Rows[0]["Lon"].ToString() != "")
                {
                    model.Lon = ds.Tables[0].Rows[0]["Lon"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Lat"] != null && ds.Tables[0].Rows[0]["Lat"].ToString() != "")
                {
                    model.Lat = ds.Tables[0].Rows[0]["Lat"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Clon"] != null && ds.Tables[0].Rows[0]["Clon"].ToString() != "")
                {
                    model.Clon = ds.Tables[0].Rows[0]["Clon"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Clat"] != null && ds.Tables[0].Rows[0]["Clat"].ToString() != "")
                {
                    model.Clat = ds.Tables[0].Rows[0]["Clat"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StaticMap"] != null && ds.Tables[0].Rows[0]["StaticMap"].ToString() != "")
                {
                    model.StaticMap = ds.Tables[0].Rows[0]["StaticMap"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Zoom"] != null && ds.Tables[0].Rows[0]["Zoom"].ToString() != "")
                {
                    model.Zoom = ds.Tables[0].Rows[0]["Zoom"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region 设置地图信息
        /// <summary>
        /// 设置地图信息
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
       public int SetMapInfo(SYH.User.Model.UserMap map)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append("update User_Map set ");
            strsql.Append(" Lon=@Lon,Lat=@Lat,Clon=@Clon,Clat=@Clat,Remark=@Remark,Zoom=@Zoom ");
            strsql.Append(" where UserId=@Userid ");
            SqlParameter[] parameter ={
                new SqlParameter("@Lon", SqlDbType.VarChar,50),
                new SqlParameter("@Lat",SqlDbType.VarChar,50),
                new SqlParameter("@Clon",SqlDbType.VarChar,50),
                new SqlParameter("@Clat",SqlDbType.VarChar,50),
                new SqlParameter("@Remark",SqlDbType.NVarChar,200),
                new SqlParameter("@Zoom",SqlDbType.VarChar,50),
                new SqlParameter("@Userid",SqlDbType.Int,4)
            };
            parameter[0].Value = map.Lon;
            parameter[1].Value = map.Lat;
            parameter[2].Value = map.Clon;
            parameter[3].Value = map.Clat;
            parameter[4].Value = map.Remark;
            parameter[5].Value = map.Zoom;
            parameter[6].Value = map.UserId;

            return Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strsql.ToString(), parameter);
        }

        #endregion
    }
}
